package org.racsor.cc; 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.google.appengine.api.rdbms.AppEngineDriver;

/**
 * 
 * @author racsor
 * 
 */

@SuppressWarnings("serial")
public class TestGuestbook {
    
    public static void main(String[] args) {
	System.out.println("begin");
	Connection c = null;
	try {
	    DriverManager.registerDriver(new AppEngineDriver());
	    c = DriverManager.getConnection("jdbc:google:rdbms://racsor:ccrequest55/mysql");
	    String fname = "fname";
	    String content = "content";
	    if (fname == "" || content == "") {
		System.out.println("<html><head></head><body>You are missing either a message or a name! Try again! Redirecting in 3 seconds...</body></html>");
	    } else {
		String statement = "INSERT INTO entries (guestName, content) VALUES( ? , ? )";
		PreparedStatement stmt = c.prepareStatement(statement);
		stmt.setString(1, fname);
		stmt.setString(2, content);
		int success = 2;
		success = stmt.executeUpdate();
		if (success == 1) {
		    System.out.println("<html><head></head><body>Success! Redirecting in 2 seconds...</body></html>");
		} else if (success == 0) {
		    System.out.println("<html><head></head><body>Failure! Please try again! Redirecting in 2 seconds...</body></html>");
		}
	    }
	} catch (SQLException e) {
	    e.printStackTrace();
	} finally {
	    if (c != null)
		try {
		    c.close();
		} catch (SQLException ignore) {
		}
	}
	
    }
    
}
